import fs from 'fs'
import { reflectType } from './mysqlreflect.js'
import { formatToHump } from './utils.js'
export const createForm = (columns, tableName) => {
  fs.readFile('src/scripts/config.json', 'utf8', (err, data) => {

    if (err) throw err
    const config = JSON.parse(data)
    const humpTableName = formatToHump(tableName)
    const folder = `${config.vendorFolder}/${humpTableName}`
    // verify file is exist or not
    const fileName = `${folder}/form.ts`

    if (fs.existsSync(fileName)) {
      // file is exist
      console.log(`======file is existed and ignored to created form:${tableName}=======`)
    } else {
      // file is not exist
      if (!fs.existsSync(folder)) {
        fs.mkdirSync(folder, { recursive: true }, (err) => {
          if (err) throw err
        })
      }
      const tableId = `${tableName}_id`
      const fields = [`${tableId}:number`]
      const model = [`${tableId}:0`]
      // const filters = [] // 过滤器
      let arr = []
      const imports = ''
      // let value = ''
      for (const i in columns) {
        const type = reflectType(columns[i])
        fields.push(`${columns[i].columnName}:${type}`)

        switch (type) {
          case 'number':
            if (columns[i].columnDefault !== null) {
              model.push(`${columns[i].columnName}:${Number(columns[i].columnDefault)}`)
              // value = `${JSON.stringify(arr)}`
            } else {
              model.push(`${columns[i].columnName}:0`)
              // value = 0
            }

            break
          case 'String':
          case 'string':
            if (columns[i].columnDefault !== null) {
              model.push(`${columns[i].columnName}:'${(columns[i].columnDefault)}'`)
              // value = columns[i].columnDefault
            } else {
              model.push(`${columns[i].columnName}:''`)
              // value = ''
            }
            break
          case 'Array<string>':
          case 'Array<String>':
            if ((columns[i].columnName.split('tags').slice(-1)[0] === '' && columns[i].dataType === 'varchar') || (columns[i].columnName.split('images').slice(-1)[0] === '' && columns[i].dataType === 'varchar')) {
              arr = []
            } else if (columns[i].columnDefault !== null) {
              arr = columns[i].columnDefault.split(',')
            }
            model.push(`${columns[i].columnName}:${JSON.stringify(arr)}`)
            // value = JSON.stringify(arr)
            break
        }

        // if(columns[i].isResearch){
        //   // 加入搜索
        //   filters.push(`${columns[i].columnName}:${value}`)
        // }
      }
      // filters.push(`keywords:''`)

      // const content = `${imports}\nexport interface Form{\n\t${fields.join(',\n\t')}\n}\nexport const model:Form = {\n\t${model.join(',\n\t')}\n}\nexport const filters = {\n\t${filters.join(',\n\t')}\n}`
      const content = `${imports}\nexport interface Form{\n\t${fields.join(',\n\t')}\n}\nexport const model:Form = {\n\t${model.join(',\n\t')}\n}`
      fs.writeFileSync(fileName, content, { flag: 'a' }, (err) => {
        if (err) {
          console.log('========error======')
          throw err
        } else {
          console.log(`======succeessfully created form:${tableName}=======`)
        }
      })
    }
  })
}
